﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace UtilityApp
{
    public partial class TwitterPage : PhoneApplicationPage
    {
        public TwitterPage()
        {
            InitializeComponent();
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {

            twitterBrowser.Navigate(new Uri("https://twitter.com/js100radio"));
            //twitterBrowser.NavigateToString("<html><body><h1>Hello</h1></body></html>");
            //TwitterPost t1 = new TwitterPost() { UserName="@js100radio",
            //    ImagePath="/Images/480x800.jpg",
            //    Text="Test Test Test"};
            //TwitterPost t2 = new TwitterPost() { UserName = "@js100radio", 
            //    ImagePath = "/Images/480x800.jpg", 
            //    Text = "Test Test Test" };
            //List<TwitterPost> twitterList = new List<TwitterPost>();
            //twitterList.Add(t1);
            //twitterList.Add(t2);
            //twitterLongList.ItemsSource = twitterList;

            LoadTwitterJSON();
        }
        private void LoadTwitterJSON()
        {
            
            //https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=js100radio&count=2
            WebClient client = new WebClient();
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadStringAsync(new Uri("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=js100radio&count=2"));

        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            JArray ar = JArray.Parse(e.Result);

            
            JObject jo1 = (JObject )ar[0];
            JObject jo2 = (JObject)ar[1];
            String txt1 =  (string) jo1["text"];
            String txt2 = (string) jo2["text"];
            JObject user1 = (JObject) jo1["user"];
            String username1 = (String) user1["name"];
            String profileimage = (String)user1["profile_image_url"];

            TwitterPost t1 = new TwitterPost()
            {
                UserName = username1,
                ImagePath = profileimage,
                Text = txt1
            };
            TwitterPost t2 = new TwitterPost()
            {
                UserName = username1,
                ImagePath = profileimage,
                Text = txt2
            };
            List<TwitterPost> twitterList = new List<TwitterPost>();
            twitterList.Add(t1);
            twitterList.Add(t2);
            twitterLongList.ItemsSource = twitterList;

        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            twitterBrowser.Navigate(twitterBrowser.Source);
        }
    }
}